import streamlit as st
from inscode import ocr

st.title('Image Text Extraction using Custom OCR (inscode)')

# 上传图片文件
image_file = st.file_uploader("Upload an Image", type=['jpg', 'jpeg', 'png'])

if image_file:
    st.write("Processing the uploaded image...")

    # 假设inscode.ocr可以直接处理file对象，如果不是这样，可能需要先将file对象保存到临时文件再处理
    # 这里直接调用inscode的OCR方法，参数为'image'类型和文件对象
    try:
        extracted_text = ocr('image', image_file.read())
        
        if extracted_text:
            st.success("Text extraction completed!")
            st.text_area("Extracted Text:", extracted_text, height=400)
        else:
            st.error("Failed to extract text from the image.")
    except Exception as e:
        st.error(f"An error occurred during OCR processing: {e}")
else:
    st.write("Please upload an image file to proceed.")

